package cn.itcast.service.system.impl;

import cn.itcast.dao.system.RoleDao;
import cn.itcast.domain.system.Role;
import cn.itcast.service.system.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Override
    public PageInfo findPage(String companyId, Integer page, Integer pageSize) {
        //使用分页插件
        PageHelper.startPage(page,pageSize);
        List<Role> list = roleDao.findAll(companyId);
        return new PageInfo(list);
    }

    @Override
    public List<Role> findAll(String companyId) {
        return roleDao.findAll(companyId);
    }

    @Override
    public void save(Role role) {
        roleDao.save(role);
    }

    @Override
    public void update(Role role) {
        roleDao.updateById(role);
    }

    @Override
    public Role findById(String id) {
        return roleDao.findById(id);
    }

    @Override
    public void deleteById(String id) {
        roleDao.deleteById(id);
    }

    @Override
    //涉及到多条sql语句执行,使用事务,可以在本方法上或类上添加事务注解@Transactional
    public void updateRoleModule(String roleid, String moduleIds) {
       //1.根据id,删除此角色所有模块数据
        roleDao.deleteRoleAndModuleByRoleId(roleid);

        //2.根据字符串获取模块id数据
        String[] mIds = moduleIds.split(",");

        //3.循环并保存权限信息
        for (String moduleId : mIds) {
            roleDao.saveRoleAndModule(roleid,moduleId);
        }


    }
}
